package br.com.saeng2D.util;

import it.businesslogic.ireport.JRField;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

public class Relatorio {
	public Relatorio(String eq1,String eq2 ,String eq3 ,String res1 ,String res2 ,String res3,
					Boolean imprimeAreaCliente,Boolean imprimeAreaDiagrama,Boolean imprimeEquacoes,Boolean imprimeResultados)  {
		Map params = new HashMap();
		params.put("equacao1",eq1); 
		params.put("equacao2",eq2); 
		params.put("equacao3",eq3); 
		params.put("resultado1",res1); 
		params.put("resultado2",res2);
		params.put("resultado3",res3);
		params.put("imprimeAreaCliente",imprimeAreaCliente);
		params.put("imprimeAreaDiagrama",imprimeAreaDiagrama);
		params.put("imprimeEquacoes",imprimeEquacoes);
		params.put("imprimeResultados",imprimeResultados);
		gerarPDF(params);
	}
		
	private  JasperPrint gerarRelatorio(Map params){
		JasperPrint rel = null;
		try {
			InputStream caminhoRelatorio = getClass().getResourceAsStream("/arquivojasper/Relatorio.jasper");
									
			rel = JasperFillManager.fillReport(caminhoRelatorio, params,new JRDataSource(){  
	            private int max = 1;  
	            private int cont = 0;  
	            private String valor;  
	            	  
	        public Object getFieldValue(JRField jrField) throws JRException {  
	                  
	                return "main";  
	            }  
	            public boolean next() throws JRException {  
	                if (cont++ >= max) {  
	                    //System.out.println("false");  
	                    return false;  
	                }  
	                else {  
	                  //  System.out.println("true");  
	                    return true;  
	                }          
	            }
				@Override
				public Object getFieldValue(
						net.sf.jasperreports.engine.JRField arg0)
						throws JRException {
					// TODO Auto-generated method stub
					return null;
				}  
	        });
			
			
			
			
		} catch (JRException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,e);
		}
		catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,e);
		}
		JasperViewer viewer = new JasperViewer(rel);
		
		viewer.viewReport(rel);
				
		
		return rel;
	}
	private void gerarPDF(Map params) {			
		try{
			File dirPDF = new File("/Relatorio/");
			if(!dirPDF.exists())
	            dirPDF.mkdirs();
						
			byte b[] = JasperExportManager.exportReportToPdf(gerarRelatorio(params));
			FileOutputStream outFileStream = new FileOutputStream(dirPDF+File.separator+"RelatorioResultados.pdf");

	        outFileStream.write(b);
	        outFileStream.close();
		}
		catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,e.getMessage());
		}
		catch (JRException e) {
			JOptionPane.showMessageDialog(null,e.getMessage());
		}
		catch (IOException e) {
			JOptionPane.showMessageDialog(null,e.getMessage());
		}
		catch (Exception e) {
			JOptionPane.showMessageDialog(null,e.getMessage());
		}		
	}	
	
}